package com.nut.service.impl;

import com.nut.common.Result;
import com.nut.entity.SysFile;
import com.nut.mapper.SysFileMapper;
import com.nut.service.SysFileService;
import com.nut.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

@Slf4j
@Service
public class SysFileServiceImpl implements SysFileService {

    public static final String HOST = "http://43.129.216.51/images/";

    @Autowired
    private SysFileMapper sysFileMapper;

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");

    public static final String filePath = "/opt/images/";

    @Override
    public Result<String> upload(MultipartFile multipartFile) {

        try {
//            SysFile sysFile = uploadToFastdfs(multipartFile);
            SysFile sysFile = uploadToLocal(multipartFile);
            sysFileMapper.insert(sysFile);
            return Result.ok(sysFile.getUrl());
        } catch (Exception e) {
            log.error("upload_error", e);
            return Result.fail(e.getMessage());
        }
    }

    private static String generateFileName(String ext) {
        return FORMATTER.format(LocalDateTime.now()) + (100 + ThreadLocalRandom.current().nextInt(899)) + "." + ext;
    }

    private SysFile uploadToLocal(MultipartFile multipartFile) {

        try {
            String ext = getExt(multipartFile);
            String fileName = generateFileName(ext);

            fileName = DateUtil.getNowMonth() + "/" + fileName;
            String path = filePath + fileName;
            // getCanonicalFile 可解析正确各种路径
            File dest = new File(path).getCanonicalFile();
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            // 文件写入
            multipartFile.transferTo(dest);

            SysFile sysFile = new SysFile();
            String url = HOST + fileName;
            sysFile.setName(fileName);
            sysFile.setUrl(url);
            sysFile.setSize(multipartFile.getSize());
            sysFile.setExt(ext);

            return sysFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    private SysFile uploadToFastdfs(MultipartFile multipartFile) {

        try {
            String ext = getExt(multipartFile);
//            String url = FastdfsUtil.upload(multipartFile, ext);
            String url = "dd";

            SysFile sysFile = new SysFile();
            sysFile.setUrl(url);
            sysFile.setSize(multipartFile.getSize());
            sysFile.setExt(ext);

            return sysFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getExt(MultipartFile multipartFile) {
        String originalFilename = multipartFile.getOriginalFilename();
        return originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
    }

    private String upload(MultipartFile file, String ext) {
        try {
            String fileName = generateFileName(ext);

            fileName = DateUtil.getNowMonth() + "/" + fileName;
            String path = filePath + fileName;
            // getCanonicalFile 可解析正确各种路径
            File dest = new File(path).getCanonicalFile();
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            // 文件写入
            file.transferTo(dest);
            return fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {

//        System.out.println(LocalDate.now().getYear());
//        System.out.println(LocalDate.now().getMonthValue());

    }
}
